window.onload = function () {
    load();

    // 导航栏
    $(".menu-list .first-menu").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("ul").slideToggle(500);
        $(this).siblings().find("ul").hide();
    })
    $(".menu-list .second-menu").click(function () {
        var $this = $(this);
        $(".second-menu").each(function () {
            if ($(this).hasClass("current")) {
                $(this).removeClass("current");
            }
        })
        $this.addClass("current").siblings().removeClass("current");
    })
    //  阻止事件冒泡
    $(".menu-list .first-menu ul").bind("click", function (event) {
        event.stopPropagation();
    });

    /**
     * 退出登录-弹框
     * 退出登录按钮     out
     * 遮盖层     container-log
     * 确认按钮    certain
     * 取消按钮    cancel
     */
    $('.out').click(function () {
        $('.container-log').show();
        $('.certain').click(function () {
            $('.container-log').hide();
            window.location.href = "http://localhost:8080/login";
        })
        $('.cancel').click(function () {
            $('.container-log').hide();
        })
    })


    /**
     * 点击详情
     * 详情     detail
     */
    // $('.detail').click(function () {
    //     window.location.href = "./particulars.html";
    // })

    /**
     * 弹出框的隐藏与显示
     * 删除按钮   delete
     * 显示框     container
     * 取消按钮   vanish
     * 确认按钮   decision
     * 删除成功弹框   success-tip
     */
    $(document).on("click", ".delete", function (e) {
        console.log($(this).attr("value"));
        var deleteid = $(this).attr("value");
        var da = {
            "bookId": deleteid
        }
        $('.container').show();
        // $(document).on('click','.decision',function() {
            $('.decision').off('click').on('click',function (){
                $.ajax({
                    type: 'post',
                    url: '/del',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(da),
                    success(data) {
                        console.log(data)
                        if (data == true) {
                            $('.container').hide();
                            setTimeout(function () {
                                $('.success-tip').show();
                            }, 800);
                            setTimeout(function () {
                                $('.success-tip').hide();
                            }, 1800);
                            load();
                        } else {
                            $('.container').hide();
                            setTimeout(function () {
                                $('.failure-tip').show();
                            }, 800);
                            setTimeout(function () {
                                $('.failure-tip').hide();
                            }, 1800);
                        }

                    }
                })
            })

        // })
        $('.vanish').click(function () {
            $('.container').hide();
        })
    })


    //显示数据到页面
    function finding(data) {
        var text = '';
        //表格书籍展示
        for (var i in data.list) {
            text += "<tr class='realitic-content'><td>" + data.list[i].bookName + "</td>"
                +"<td>" +data.list[i].bookISBN+"</td>"
                + "<td>" + data.list[i].author + "</td>"
                + "<td>" + data.list[i].publisher + "</td>"
                + "<td>" + data.list[i].money + "</td>"
                + "<td>" + data.list[i].category + "</td>"
                + "<td>" + data.list[i].text + "</td>"
                + "<td class='functional-borrow'>"
                + "<button class='edit sames' id='ssss' title='编辑' type='button' value='" + data.list[i].bookId + "'> 编辑</a>"
                + "<button class='detail sames' title='详细' type='button' value='" + data.list[i].bookId + "'> 详请</button>"
                + "<button class='delete sames' title='删除' value='" + data.list[i].bookId + "'> 删除</button>"
                + "</td></tr>"
            // $('.ss').html(text)
        }
        $('.ss').html(text)

        //分页数据展示
        if (!data.hasPreviousPage) {//处理前一页按钮
            var p = "<a id='passage' class='page-prev' value='" + data.pageNum + "'>&lt;</a>";
            // console.log("**********",data.pageNum)
        } else {
            var p = "<a id='passage' class='page-prev' value='" + data.prePage + "'>&lt;</a>";
            // console.log("-----------",data.prePage)
        }
        var g = "'page-current':''";
        var flag;
        for (var i = 1; i <= data.pages; i++) {
            flag = '';
            if (i == data.pageNum) {
                flag = "page-current";
            }
            //当前页面的按钮变成深色
            p += "<a class='" + flag + "' id='passage' value='" + i + "'>" + i + "</a>";
        }
        //处理后一页按钮
        if (data.hasNextPage) {//当当前页面由下一页的时候
            p += "<a id='passage' value='" + data.nextPage + "'>&gt;</a>"
            // console.log(data.nextPage)
        } else {
            p += "<a id='passage' value='" + data.pageNum + "'>&gt;</a>"
            // console.log(data.pageNum)
        }
        $(".page-normal").html(p);

        //总数提示
        var a = '';
        a = "<p >当前 <span text='" + data.pageNum + "'>" + data.pageNum + "</span> 页  " +
            "总 <span text='" + data.pages + "'>" + data.pages + "</span> 页  " +
            "共 <span text=‘" + data.total + "’>" + data.total + "</span> 条记录" +
            "</p>"
        $('.proceeding').html(a);
    }


    /**
     * 点击页码
     * 页码  passage
     */
    $(document).on("click", "#passage", function (e) {
        // console.log($(this).attr("value"));
        var no = $(this).attr("value");
        var book = $('#bookName').val();
        var auth = $('#author').val();
        var kind = $('#kind').val();
        var data = {
            "pageNum": no,
            "bookName": book,
            "author": auth,
            "category": kind
        }
        $.ajax({
            type: 'get',
            url: '/conditionFind',
            dataType: 'json',
            data: data,
            success(data) {
                finding(data)
            }
        })


    })

    //首页数据展示
    function load() {
        $.ajax({
            type: "get",
            url: '/conditionFind',
            dataType: "json",
            success(data) {
                finding(data)
            }
        })
    }

    /**
     * 搜索框查询
     * 搜索    admit
     * 书名    bookName
     * 作者    author
     * 类别    kind
     */
    $('#admit').click(function () {

        console.log($('#bookName').val())
        var book = $('#bookName').val();
        var auth = $('#author').val();
        var kind = $('#kind').val();
        var data = {
            "bookName": book,
            "author": auth,
            "category": kind
        }
        $.ajax({
            type: 'get',
            url: '/conditionFind',
            data: data,
            dataType: 'json',
            success(data) {
                finding(data)
            }
        })
    })

    /**
     * 清空按钮
     * 清空     clearly
     * 书名    bookName
     * 作者    author
     * 类别    kind
     */
    $('#clearly').click(function () {
        console.log("点击了");
        $('#bookName').val('');
        $('#author').val('');
        $('#kind').val('');
        load();
    })

    /**
     * 点击编辑进入新的页面
     * 按钮名     edit
     */
    $(document).on('click', '.edit',function () {
        $('.frame').show();
        var editId = $(this).attr('value');
        var data = {
            bookId: editId
        }
        $.ajax({
            type: 'get',
            url: '/bookInfo',
            dataType: 'json',
            data: data,
            success(data) {
                $('.bookName').val(data.bookName)
                $('.bookNumber').val(data.bookISBN)
                $('.bookAuthor').val(data.author)
                $('.bookPublish').val(data.publisher)
                $('.bookPrice').val(data.money)
                $('.bookType').val(data.category)
                $('.bookContext').val(data.text)

            }
        })

        $(".update").off('click').on('click', function () {
            var bookName = $('.bookName').attr('value');
            var bookNumber = $('.bookNumber').attr('value');
            var bookAuthor = $('.bookAuthor').attr('value');
            var bookPublish = $('.bookPublish').attr('value');
            var bookPrice = $('.bookPrice').attr('value');
            var bookType = $('.bookType').attr('value');
            var bookContext = $('.bookContext').attr('value');
            var data = {
                bookId: editId,
                bookName: bookName,
                bookISBN: bookNumber,
                author: bookAuthor,
                publisher: bookPublish,
                money: bookPrice,
                category: bookType,
                text: bookContext
            }
            // console.log(bookName)
            $.ajax({
                type: 'post',
                url: '/updateBookInfo',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(data),
                success(data) {
                    if (data == true) {
                        $('.frame').hide();
                        setTimeout(function () {
                            $('.success-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.success-tip').hide();
                        }, 1800);
                        load();
                    } else {
                        $('.frame').hide();
                        setTimeout(function () {
                            $('.failure-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.failure-tip').hide();
                        }, 1800);
                    }
                }
            })
        })


        $('.removal').click(function () {
            $('.frame').hide();
        })
    })


}

